package com.westar.core.controller;

import com.alibaba.fastjson.JSONObject;
import com.westar.base.pojo.WestarResult;
import com.westar.core.services.LoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 系统认证标识获取
 */
@RestController
@EnableAutoConfiguration
@RequestMapping("/")
@Api("用户认证标识获取")
public class LoginController {

    private static Logger logger = LoggerFactory.getLogger(LoginController.class);

    @Autowired
    private LoginService loginService;

    @ApiOperation(value  ="测试系统是否正常部署",response = String.class,notes ="set name")
    @RequestMapping(value ="/hello",method = RequestMethod.GET)
    public String hello() {
        return "hello";
    }

    /**
     * 获取唯一标识
     *
     * @param username 用户名
     * @param password 密码
     * @return
     */
    @ApiOperation("认证标识获取")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name="username",dataType="String",required=true,value="一体化平台账号"),
            @ApiImplicitParam(paramType="query",name="password",dataType="String",required=true,value="一体化平台密码")
    })
    @RequestMapping(value = "/getToken", method = RequestMethod.GET,produces = "application/json")
    public WestarResult getToken( @RequestParam("username")String username, @RequestParam("password") String password) {

        WestarResult westarResult = null;
        if (StringUtils.isEmpty( username )) {
            westarResult = WestarResult.build( -1,"用户名不能为空" );
        }else if(StringUtils.isEmpty(password)){
            westarResult = WestarResult.build( -1,"密码不能为空" );
        }else{
            //已经输入的密码集合
            List<String> prePasswdList = new ArrayList<>();
            westarResult = loginService.getToken( username, password ,0,prePasswdList);

        }
        return westarResult;
    }
}
